<?php
class timezone_support extends panel {
	var $name = 'Timezone Support';
	
	public static function display() {
		global $cndb;
		if($_REQUEST['cn_tz_update']) {
			cn_set_option('cn_tz_by', $_REQUEST['cn_tz_by']);
			cn_set_option('cn_tz_allow', serialize($_REQUEST['cn_tz_allow']));
			cn_set_option('cn_tz_default', $_REQUEST['cn_tz_default']);
			cn_set_option('cn_tz_listing', $_REQUEST['cn_tz_listing']);
			$listing = cn_get_option('cn_tz_listing');
			/** Add custom field to user */
			if($_REQUEST['cn_tz_by'] == 'user') {
				$cndb->query("INSERT INTO `cn_listing_fields` (`lf_display_name`, `lf_data_type`, `lf_column`, `lf_ltype`) VALUES('Timezone', 'Timezone', 'user_timezone', '0')");
				$cndb->query("ALTER TABLE `cn_users` ADD `user_timezone` int(11)");
				
				if($listing) {
					$cndb->query("DELETE FROM `cn_listing_fields` WHERE `lf_data_type` = 'Timezone' AND `lf_ltype`='$listing'");
					if($cndb->column_exists('cn_listing_type'.$listing, 'l'.$listing.'_timezone')) $cndb->query("ALTER TABLE `cn_listing_type$listing` DROP COLUMN `l".$listing."_timezone`");
					
				}
			}
			else {
				if($cndb->column_exists('cn_users', 'user_timezone')) $cndb->query("ALTER TABLE `cn_users` DROP COLUMN `user_timezone`");
	
				$cndb->query("DELETE FROM `cn_listing_fields` WHERE `lf_data_type` = 'Timezone' AND `lf_ltype`='0'");
				
				
				/** Add field to auxilliary listing data table */
				
				if(!$cndb->column_exists('cn_listing_type'.$listing, 'l'.$listing.'_timezone')) $cndb->query("ALTER TABLE `cn_listing_type$listing` ADD `l".$listing."_timezone` int(11)");
				$cndb->query("INSERT INTO `cn_listing_fields` (`lf_display_name`, `lf_data_type`, `lf_column`, `lf_ltype`) VALUES('Timezone', 'Timezone', 'l".$listing."_timezone', '$listing')");
			}
			?><p class="alert-success">Settings updated successfully.</p><?php
		}
		$listing = cn_get_option('cn_tz_listing');
		$default = cn_get_option('cn_tz_default');
		
		$allowed = cn_get_option('cn_tz_allow');
		if($allowed) $allowed_timezones = unserialize($allowed);
		if(!$allowed_timezones) $allowed_timezones = array();
		?>
		<div class="one-column">
			<div class="box">
				<h3>Timezone Configuration</h3>
				<div class="box-content">
					<form action="<?=$_SERVER['PHP_SELF']?><?=query_string()?>" method="post">
						<label><input type="radio" <?php echo (cn_get_option('cn_tz_by') == 'listing') ? 'checked="checked"' : ''; ?> name="cn_tz_by" value="listing"/> Current time is set by the listing currently being browsed</label>
						<label><input type="radio" <?php echo (cn_get_option('cn_tz_by') == 'user') ? 'checked="checked"' : ''; ?> name="cn_tz_by" value="user"/> Current time is set by the user's selected timezone</label>
						<label><input type="radio" <?php echo (cn_get_option('cn_tz_by') == 'custom') ? 'checked="checked"' : ''; ?> name="cn_tz_by" value="custom"/> Current time is set within your own code ($_SESSION['user_timezone'])</label>
						
						<p><label>Default timezone is: 
							<select name="cn_tz_default">
								<?php $results = $cndb->get_results("SELECT * FROM `cn_timezones` ORDER BY `tz_name`");
								foreach($results as $result) :?>
								<option <?php echo ($default == $result->tz_id) ? 'selected="selected"' : '';?> value="<?=$result->tz_id?>"><?=$result->tz_name?></option>
								<?php endforeach;?>
							</select>
						</label>
						<label>Attach timezones to:
							<select name="cn_tz_listing">
								<?php $results = $cndb->get_results("SELECT * FROM `cn_listing_types` ORDER BY `lt_title`");
								foreach($results as $result) : ?>
								<option <?php echo ($default == $result->lt_id) ? 'selected="selected"' : '';?> value="<?=$result->lt_id?>"><?=$result->lt_title?></option>
								<?php endforeach; ?>
							</select> (Only viable if you select "Current time is set by the listing..." above)
						</label></p>
						
						<h4>Allowed Timezones</h4>
						<p><i>Timezones are taken from and used in conjunction with PHP's DateTime class. Some (like America & United States) have overlapping timezones.</i></p>
						<label><input type="checkbox" <?php echo (in_array('Africa', $allowed_timezones)) ? 'checked="checked"' : ''; ?> name="cn_tz_allow[]" value="Africa"/> Africa</label>
						<label><input type="checkbox" <?php echo (in_array('America', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="America"/> America</label>
						<label><input type="checkbox" <?php echo (in_array('Antarctica', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="Antarctica"/> Antarctica</label>
						<label><input type="checkbox" <?php echo (in_array('Asia', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="Asia"/> Asia</label>
						<label><input type="checkbox" <?php echo (in_array('Atlantic', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="Atlantic"/> Atlantic Islands</label>
						<label><input type="checkbox" <?php echo (in_array('Australia', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="Australia"/> Australia</label>
						<label><input type="checkbox" <?php echo (in_array('Canada', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="Canada"/> Canada</label>
						<label><input type="checkbox" <?php echo (in_array('Europe', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="Europe"/> Europe</label>
						<label><input type="checkbox" <?php echo (in_array('Indian', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="Indian"/> Indian Ocean</label>
						<label><input type="checkbox" <?php echo (in_array('Pacific', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="Pacific"/> Pacific Islands</label>
						<label><input type="checkbox" <?php echo (in_array('US', $allowed_timezones)) ? 'checked="checked"' : ''; ?>name="cn_tz_allow[]" value="US"/> United States</label>
						<br />
						<input type="submit" value="Update Settings" class="small-button" name="cn_tz_update"/>
						
					</form>
				</div>
			</div>
		</div>
	
		<?php
	}
}

?>